//
//  MJ_SegmentButtonView.m
//  UEAssistant
//
//  Created by mac on 2018/1/3.
//  Copyright © 2018年 youyixinxi. All rights reserved.
//

#import "MJ_SegmentButtonView.h"
#import "UIColor+Extend.h"
#import <Masonry.h>
@interface MJ_SegmentButtonView ()

@property (strong, nonatomic)UIButton *leftButton;
@property (strong, nonatomic)UIButton *rigthButton;

@property (strong, nonatomic)UIView *line;

@property (assign, nonatomic)NSInteger lastClickIndex;


@property (strong, nonatomic)UIButton *centerButton;
@end

@implementation MJ_SegmentButtonView


- (UIButton *)leftButton {
    if (!_leftButton) {
        _leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftButton setTitle:@"待抢单" forState:UIControlStateNormal];
        [_leftButton setTitleColor:[UIColor colorWithHexString:@"#0084ff"] forState:UIControlStateSelected];
        [_leftButton setTitleColor:[UIColor colorWithHexString:@"#adb2bb"] forState:UIControlStateNormal];
        _leftButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_leftButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        _leftButton.tag = 999;
        
    }
    return _leftButton;
}

- (UIButton *)centerButton {
    if (!_centerButton) {
        _centerButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_centerButton setTitle:@"试译中" forState:UIControlStateNormal];
        [_centerButton setTitleColor:[UIColor colorWithHexString:@"#0084ff"] forState:UIControlStateSelected];
        [_centerButton setTitleColor:[UIColor colorWithHexString:@"#adb2bb"] forState:UIControlStateNormal];
        _centerButton.titleLabel.font = [UIFont systemFontOfSize:14];
        [_centerButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        _centerButton.tag = 997;
        
    }
    return _centerButton;
}

- (UIButton *)rigthButton {
    if (!_rigthButton) {
        _rigthButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rigthButton setTitle:@"进行中" forState:UIControlStateNormal];
        [_rigthButton setTitleColor:[UIColor colorWithHexString:@"#0084ff"] forState:UIControlStateSelected];
        [_rigthButton setTitleColor:[UIColor colorWithHexString:@"#adb2bb"] forState:UIControlStateNormal];
        _rigthButton.titleLabel.font = [UIFont systemFontOfSize:14];
        _rigthButton.tag = 998;
        [_rigthButton addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rigthButton;
}
- (UIView *)line {
    if (!_line) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = [UIColor colorWithHexString:@"#0084ff"];
    }
    return _line;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.leftButton];
        [self addSubview:self.rigthButton];
        [self addSubview:self.line];
        _lastClickIndex = 0;
        [self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.mas_equalTo(Screen_Width/4.0);
            make.left.mas_equalTo(Screen_Width/8.0);
        }];
        
        [self.rigthButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.mas_equalTo(Screen_Width/4.0);
            make.left.equalTo(self.leftButton.mas_right).offset(Screen_Width/4.0);
        }];
        self.leftButton.selected = YES;
        self.line.frame = CGRectMake(Screen_Width/8, frame.size.height-2.0, Screen_Width/4.0, 2.0);
    }
    return self;
}

- (void)buttonPressed:(UIButton *)sender {
    
    if (sender.tag == 999) {
        
        if (_lastClickIndex != 0) {//待抢单
            //记录上次点击的按钮，防止重复点击
            _lastClickIndex = 0;
            [_delegate clickSegmentBtnWithType:MJSegmentTypeWaitOrder];
            self.leftButton.selected = YES;
            self.rigthButton.selected = NO;
            self.centerButton.selected = NO;
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self.leftButton.titleLabel);
                make.centerX.equalTo(self.leftButton.titleLabel);
                make.bottom.equalTo(self);
                make.height.mas_equalTo(2);
            }];
//            __weak typeof(self) weakSelf = self;
//
//            [UIView animateWithDuration:0.25 animations:^{
//                CGRect frame = weakSelf.leftButton.titleLabel.frame;
//               self.line.frame = CGRectMake(frame.origin.x, self.frame.size.height-2.0, frame.size.width, 2.0);
//            }];
        }
        
    }else if(sender.tag == 998){//进行中
        if (_lastClickIndex != 1) {
            //记录上次点击的按钮，防止重复点击
            _lastClickIndex = 1;
            
            [_delegate clickSegmentBtnWithType:MJSegmentTypeOngoing];
            self.leftButton.selected = NO;
            self.rigthButton.selected = YES;
            self.centerButton.selected = NO;
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self.rigthButton.titleLabel);
                make.centerX.equalTo(self.rigthButton.titleLabel);
                make.bottom.equalTo(self);
                make.height.mas_equalTo(2);
            }];
//            __weak typeof(self) weakSelf = self;
//            [UIView animateWithDuration:0.25 animations:^{
//                CGRect frame = weakSelf.rigthButton.titleLabel.frame;
//                self.line.frame = CGRectMake(frame.origin.x, self.frame.size.height-2.0, frame.size.width, 2.0);
//            }];
        }
    }else{
        if (_lastClickIndex != 2) {
            //记录上次点击的按钮，防止重复点击
            _lastClickIndex = 2;
            
            [_delegate clickSegmentBtnWithType:MJSegmentTypeTranslation];
            self.leftButton.selected = NO;
            self.rigthButton.selected = NO;
            self.centerButton.selected = YES;
            [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.equalTo(self.centerButton.titleLabel);
                make.centerX.equalTo(self.centerButton.titleLabel);
                make.bottom.equalTo(self);
                make.height.mas_equalTo(2);
            }];
//            __weak typeof(self) weakSelf = self;
//            [UIView animateWithDuration:0.25 animations:^{
//                CGRect frame = weakSelf.centerButton.titleLabel.frame;
//                self.line.frame = CGRectMake(frame.origin.x, self.frame.size.height-2.0, frame.size.width, 2.0);
//            }];
        }

    }
}

- (void)setSelectedIndex:(NSInteger)selectedIndex {
    _selectedIndex = selectedIndex;
    if (_selectedIndex == 0) {
        self.leftButton.selected = YES;
        self.rigthButton.selected = NO;
        self.centerButton.selected = NO;
        _lastClickIndex = 0;
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.leftButton.titleLabel);
            make.centerX.equalTo(self.leftButton.titleLabel);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(2);
        }];
    }else if (_selectedIndex == 1){
        
        self.leftButton.selected = NO;
        self.rigthButton.selected = YES;
        self.centerButton.selected = NO;
        _lastClickIndex = 1;
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.rigthButton.titleLabel);
            make.centerX.equalTo(self.rigthButton.titleLabel);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(2);
        }];
        
    }else{
        
        self.leftButton.selected = NO;
        self.rigthButton.selected = NO;
        self.centerButton.selected = YES;
        _lastClickIndex = 2;
        
        [self.line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self.centerButton.titleLabel);
            make.centerX.equalTo(self.centerButton.titleLabel);
            make.bottom.equalTo(self);
            make.height.mas_equalTo(2);
        }];
    }
}

- (void)setIsGroupSelected:(BOOL)isGroupSelected {
    _isGroupSelected = isGroupSelected;
    if (!isGroupSelected) {
        [self addSubview:self.leftButton];
        [self addSubview:self.centerButton];
        [self addSubview:self.rigthButton];

        [self.leftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.mas_equalTo(Screen_Width/3.0);
            make.left.equalTo(self);
        }];

        [self.centerButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.equalTo(self.leftButton.mas_width);
            make.left.equalTo(self.leftButton.mas_right).offset(0);
        }];

        [self.rigthButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.equalTo(self.leftButton.mas_width);
            make.left.equalTo(self.centerButton.mas_right).offset(0);
        }];
        
    }else{
        [self.centerButton removeFromSuperview];
        [self addSubview:self.leftButton];
        [self addSubview:self.rigthButton];
        [self.leftButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.mas_equalTo(Screen_Width/4.0);
            make.left.mas_equalTo(Screen_Width/8.0);
        }];
        
        [self.rigthButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self);
            make.bottom.equalTo(self).offset(2.0);
            make.width.mas_equalTo(Screen_Width/4.0);
            make.left.equalTo(self.leftButton.mas_right).offset(Screen_Width/4.0);
        }];
    }
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(self.leftButton.titleLabel);
        make.centerX.equalTo(self.leftButton.titleLabel);
        make.bottom.equalTo(self);
        make.height.mas_equalTo(2);
    }];
}

@end
